﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections;

namespace DienThoai
{
    /// <summary>
    /// Interaction logic for TraCuu.xaml
    /// </summary>
    public partial class TraCuu : Window
    {
        DienThoaiDataContext db = new DienThoaiDataContext();
        public TraCuu()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Handle event when form is loaded
        /// </summary>
        /// <param name="sender">Object that sends this event</param>
        /// <param name="e">Event argument</param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var query = from manu in db.Manufacrurers
                        select manu.ManufacrurerName;
            foreach (var item in query)
            {
                cbTenHang.Items.Add(item);
            }
            query = from mem in db.MemoryCards
                    select mem.Size.ToString();
            foreach (var item in query)
            {
                cbTheNho.Items.Add(item);
            }
            query = from sim in db.MultiSims
                    select sim.SimQuantity.ToString();
            foreach (var item in query)
            {
                cbSim.Items.Add(item);
            }
        }
        private Queue<SearchResult> queue;
        private void btnTimKiem_Click(object sender, RoutedEventArgs e)
        {
            if (txtTuyChon.Text == "")
            {
                var query = from t in db.Phones
                            join t1 in db.Products
                            on t.PhoneID equals t1.ProductID
                            join t2 in db.Manufacrurers
                            on t1.ManufacturerID equals t2.ManufacrurerID
                            where t2.ManufacrurerName == cbTenHang.SelectedItem.ToString()
                            select t2.ManufacrurerName;
                try
                {
                    lbTenHang.Content = query.Single().ToString();
                }
                catch
                {
                    lbTenHang.Content = "Không có kết quả";
                }
            }
            else
            {
                queue = new Queue<SearchResult>();
                SearchResult result = new SearchResult();
                if (cbTenHang.SelectedIndex == 0)
                {
                    var query = from t in db.Phones
                                select t;
                    foreach (var item in query)
                    {
                        result.SetValue(item.Product.Manufacrurer.ManufacrurerName, item.Product.Model, item.Product.Quantity.ToString(), item.Product.Price.ToString(), true, true, item.MemoryCard.Size, true, true,
                            true, true, true, true, item.MultiSim.SimQuantity.ToString(), item.TouchScreenDetail.Point, item.CameraDetail.MegaPixel);
                        queue.Enqueue(result);
                    }


                }
            }

        }

        private void imgAll_MouseDown(object sender, MouseButtonEventArgs e)
        {
  

            var query = from i in db.Accessories
                        where i.AccessoryKind.Name == "Loa"
                        select new
                        {
                            i.Detail,
                            i.Product.Price,
                            i.Product.Img
                        };
            foreach (var item in query)
            {
                ResultAccessory ucResult = new ResultAccessory();
                ucResult.lbAccessoryName.Content = item.Detail;
                ucResult.lbAccessoryPrice.Content = item.Price;
                Uri uri = new Uri(item.Img, UriKind.Relative);

                ImageSource imgSource = new BitmapImage(uri);

                ucResult.imgAccessoryImage.Source = imgSource;
                wpResultAccessory.Children.Add(ucResult);
            }
        }

        private void imgHeadPhone_MouseDown(object sender, MouseButtonEventArgs e)
        {
            var query = from i in db.Accessories
                        where i.AccessoryKind.Name == "Head phone"
                        select new
                        {
                            i.Detail,
                            i.Product.Price,
                            i.Product.Img
                        };
            foreach (var item in query)
            {
                ResultAccessory ucResult = new ResultAccessory();
                ucResult.lbAccessoryName.Content = item.Detail;
                ucResult.lbAccessoryPrice.Content = item.Price;
                Uri uri = new Uri(item.Img, UriKind.Relative);

                ImageSource imgSource = new BitmapImage(uri);

                ucResult.imgAccessoryImage.Source = imgSource;
                wpResultAccessory.Children.Add(ucResult);
            }
        }

        private void imgPin_MouseDown(object sender, MouseButtonEventArgs e)
        {
            var query = from i in db.Accessories
                        where i.AccessoryKind.Name == "Pin"
                        select new
                        {
                            i.Detail,
                            i.Product.Price,
                            i.Product.Img
                        };
            foreach (var item in query)
            {
                ResultAccessory ucResult = new ResultAccessory();
                ucResult.lbAccessoryName.Content = item.Detail;
                ucResult.lbAccessoryPrice.Content = item.Price;
                Uri uri = new Uri(item.Img, UriKind.Relative);

                ImageSource imgSource = new BitmapImage(uri);

                ucResult.imgAccessoryImage.Source = imgSource;
                wpResultAccessory.Children.Add(ucResult);
            }
        }

        private void imgUSB_MouseDown(object sender, MouseButtonEventArgs e)
        {
            var query = from i in db.Accessories
                        where i.AccessoryKind.Name == "USB"
                        select new
                        {
                            i.Detail,
                            i.Product.Price,
                            i.Product.Img
                        };
            foreach (var item in query)
            {
                ResultAccessory ucResult = new ResultAccessory();
                ucResult.lbAccessoryName.Content = item.Detail;
                ucResult.lbAccessoryPrice.Content = item.Price;
                Uri uri = new Uri(item.Img, UriKind.Relative);

                ImageSource imgSource = new BitmapImage(uri);

                ucResult.imgAccessoryImage.Source = imgSource;
                wpResultAccessory.Children.Add(ucResult);
            }
        }

        private void imgBaoDa_MouseDown(object sender, MouseButtonEventArgs e)
        {
            var query = from i in db.Accessories
                        where i.AccessoryKind.Name == "Bao da"
                        select new
                        {
                            i.Detail,
                            i.Product.Price,
                            i.Product.Img
                        };
            foreach (var item in query)
            {
                ResultAccessory ucResult = new ResultAccessory();
                ucResult.lbAccessoryName.Content = item.Detail;
                ucResult.lbAccessoryPrice.Content = item.Price;
                Uri uri = new Uri(item.Img, UriKind.Relative);

                ImageSource imgSource = new BitmapImage(uri);

                ucResult.imgAccessoryImage.Source = imgSource;
                wpResultAccessory.Children.Add(ucResult);
            }
        }



    }
}
